<?php
class Page
{
	/**
	*
	*page标签
	*@var string
	*/
	var $page_Name = "page";
	/**
	*
	*下一页
	*@var string
	*/
	var $next_Page;
	/**
	*
	*上一页
	*@var string
	*/
	var $pre_Page;
	/**
	*
	*首页
	*@var string
	*/
	var $first_Page;
	/**
	*尾页
	*@var string
	*/
	var $last_Page;
	/**
	*
	*上一分页条
	*@var string
	*/
	var $pre_Bar;
	/**
	*
	*下一分页条
	*@var string
	*/
	var $next_Bar;
	/**
	*
	*隐藏分页左分隔条
	*@var string
	*/
	var $format_Left = '[';
	/**
	*
	*隐藏分页右分隔条
	*@var string
	*/
	var $format_Right = ']';
	/**
	*
	*是否支持ajax分页
	*@var boolean
	*/
	var $is_Ajax = false;
	/**
	*
	*控制记录条的个数
	*@var int
	*/
	var $pagebarNum = 10;
	/**
	*
	*总页数
	*@var int
	*/
	var $totalPage = 0;
	/**
	*
	*ajax动作名
	*@var string
	*/
	var $ajax_action_Name = '';
	/**
	*
	*当前页
	*@var int
	*/
	var $nowIndex = 1;
	/**
	*
	*Url地址头
	*@var string
	*/
	var $page_Url = "";
	/**
	*
	*Mysql Limit值
	*@var int
	*/
	var $offSet = 0;
	/**
	*
	*构造函数
	*@param array[totla]
	*@param array[perpage]
	*@param array[nowindex]
	*@param array[url]
	*@param array[ajax]
	*/
	function __construct($array)
	{
		if(is_array($array))
		{
			 if(!array_key_exists('total',$array))$this->error(__FUNCTION__,'need a param of total');
			 $total=intval($array['total']);
			 $perpage=(array_key_exists('perpage',$array))?intval($array['perpage']):10;
			 $nowindex=(array_key_exists('nowindex',$array))?intval($array['nowindex']):'';
			 $url=(array_key_exists('url',$array))?$array['url']:'';
		}
		else
		{
			 $total=$array;
			 $perpage=10;
			 $nowindex='';
			 $url='';
		}
		  if((!is_int($total))||($total<0))$this->error(__FUNCTION__,$total.' is not a positive integer!');
		  if((!is_int($perpage))||($perpage<=0))$this->error(__FUNCTION__,$perpage.' is not a positive integer!');
		  if(!empty($array['page_name']))$this->set('page_Name',$array['page_Name']);//设置pagename
		  $this->set_NowIndex($nowindex);//设置当前页
		  $this->set_Url($url);//设置链接地址
		  $this->totalPage=ceil($total/$perpage);
		  $this->offSet=($this->nowIndex-1)*$perpage;
		  if(!empty($array['ajax']))$this->open_ajax($array['ajax']);//打开AJAX模式
	}
	 /**
	  * 设定类中指定变量名的值，如果改变量不属于这个类，将throw一个exception
	  *
	  * @param string $var
	  * @param string $value
	  */
	 function set($var,$value)
	 {
	  //if(in_array($var,get_object_vars($this)))
		 $this->$var=$value;
	  //else {
	   //$this->error(__FUNCTION__,$var." does not belong to PB_Page!");
	  }
	/**
	*
	*是否打开Ajax分页
	*@param string action //默认触发动作
	*
	*/
	function open_Ajax($action)
	{
		$this->is_Ajax = true;
		$this->ajax_action_Name = $action;
	}
	/**
	*
	*获取下一页
	*@param string sStyle
	*@return string
	*/
	function next_Page($sStyle = '',$fStyle='')
	{
		if($this->nowIndex<$this->totalPage)
		{
			return $this->get_Link($this->get_Url($this->nowIndex+1),$this->next_Page,$sStyle)."\n";
		}
		return '<span class="'.$fStyle.'">'.$this->next_Page.'</span>'."\n";
	}
	/**
	*
	*获取上一页
	*@param string sStyle
	*@return string
	*/
	function pre_Page($sStyle = '',$fStyle='')
	{
		if($this->nowIndex>1)
		{
			return $this->get_Link($this->get_Url($this->nowIndex-1),$this->pre_Page,$sStyle)."\n";
		}
		return '<span class="'.$fStyle.'">'.$this->pre_Page.'</span>'."\n";
	}
	/**
	*
	*获取首页
	*@param string sStyle
	*@return string
	*/
	function first_Page($sStyle = '')
	{
		if($this->nowIndex==1)
		{
			return '<span class="'.$sStyle.'">'.$this->first_Page.'</span>'."\n";
		}
		return $this->get_Link($this->get_Url(1),$this->first_Page,$sStyle)."\n";
	}
	/**
	*
	*获取尾页
	*@param string sStyle
	*@return string
	*/
	function last_Page($sStyle = '')
	{
		if($this->nowIndex==$this->totalPage)
		{
			return '<span>'.$this->last_Page.'</span>'."\n";
		}
		return $this->get_Link($this->get_Url($this->totalPage),$this->last_Page,$sStyle)."\n";
	}
	/**
	*
	*页码隐藏分页实现
	*@param string sStyle
	*@param string nowIndexStyle
	*@return string
	*/
	function nowBar($sStyle = '', $nowIndexStyle = '')
	{
		$plus = ceil($this->pagebarNum/2);
		if($this->pagebarNum-$plus+$this->nowIndex>$this->totalPage)$plus = ($this->pagebarNum-$this->totalPage+$this->nowIndex);			
		$begin = $this->nowIndex-$plus+1;
		$begin = ($begin>=1) ? $begin:1;
		$return = '';
		for($i=$begin;$i<$begin+$this->pagebarNum;$i++)
		{
			if($i<=$this->totalPage)
			{
				if($i!=$this->nowIndex)
					$return.= $this->get_Text($this->get_Link($this->get_Url($i),$i,$sStyle));
				else
					$return.= $this->get_Text('<span class="'.$nowIndexStyle.'">'.$i.'</span>');
			}
			else
			{
				break;
			}
			$return.="\n";
		}
		unset($begin);
		return $return;
	}
	/**
	*
	*获取跳转按钮代码
	*@return string
	*/
	function select()
	{
		if($this->is_Ajax)
		{
$return="<Select name=\"PB_Page_Select\" onchange=\"javascript:".$this->ajax_action_Name."('" . $this->page_Url . "'+this.options[this.selectedIndex].value)\">";
		}
		else
		{
			$return="<Select name=\"PB_Page_Select\" onchange=\"javascript:window.location='" . $this->page_Url . "'+this.options[this.selectedIndex].value;\">";
		}
		for($i=1;$i<=$this->totalPage;$i++)
		{
			$return .= "<option value=\"{$i}\"";
			if($i==$this->nowIndex) $return .= " Selected";
			$return .= ">".$i."</option>";
		}
		unset($i);
		$return.="</Select>";
		return $return;
	}
	/**
	*
	*获取Mysql Limit值
	*@return string
	*/
	function offSet()
	{
		return $this->offSet;
	}
	/**
	*
	*控制分页显示
	*@param int iMode
	*@return string
	*/
	function show($iMode = 1)
	{
		switch($iMode)
		{
			case '1':
				$this->next_Page = '下一页';
				$this->pre_Page  = '上一页';
				return $this->pre_Page().$this->nowBar().$this->next_Page().'第'.$this->select().'页';
				break;
			case '2':
				$this->next_Page = '下一页';
				$this->pre_Page  = '上一页';
				$this->first_Page= '首页';
				$this->last_Page = '尾页';
				return $this->first_Page().$this->pre_Page().'[第'.$this->nowIndex.'页]'.$this->next_Page().$this->last_Page().'第'.$this->select().'页';
				break;
			case '3':
				$this->next_Page = "下一页";
				$this->pre_Page  = "上一页";
				$this->first_Page= "首页";
				$this->last_Page = "尾页";
				return $this->first_Page().$this->pre_Page().$this->next_Page().$this->last_Page();
				break;
			case '4':
				$this->next_Page = '下一页';
				$this->pre_Page  = '上一页';
				return $this->pre_Page().$this->nowBar().$this->next_Page();
				break;
			case '5':
				return $this->pre_Bar.$this->nowBar().$this->next_Page();
				break;
		}
	}
	 /**
	  * 设置url头地址
	  * @param: String $sUrl
	  * @return boolean
	  */
	 function set_Url($sUrl="")
	 {
	  if(!empty($sUrl)){
		  //手动设置
	   $this->page_Url=$sUrl.((stristr($sUrl,'?'))?'&':'?').$this->page_Name."=";
	  }else{
		  //自动获取
	   if(empty($_SERVER['QUERY_STRING'])){
		   //不存在QUERY_STRING时
		$this->url=$_SERVER['REQUEST_URI']."?".$this->page_Name."=";
	   }else{
		   //
		if(stristr($_SERVER['QUERY_STRING'],$this->page_Name.'=')){
			//地址存在页面参数
		 $this->page_Url=str_replace($this->page_Name.'='.$this->nowIndex,'',$_SERVER['REQUEST_URI']);
		 $last=$this->page_Url[strlen($this->page_Url)-1];
		 if($last=='?'||$last=='&'){
			 $this->page_Url.=$this->page_Name."=";
		 }else{
			 $this->page_Url.='&'.$this->page_Name."=";
		 }
		}else{			
		 $this->page_Url=$_SERVER['REQUEST_URI'].'&'.$this->page_Name.'=';
		}    
	   }
	  }
	 }
	/**
	*
	*设置当前页
	*@param int nowIndex
	*/
	function set_NowIndex($iNowIndex)
	{
		if(empty($iNowIndex))
		{
			if(isset($_GET[$this->page_Name]))
			{
				$this->nowIndex = intval($_GET[$this->page_Name]);
			}
			else
			if(isset($_POST[$this->page_Name]))
			{
				$this->nowIndex = intval($_POST[$this->page_Name]);
			}
		}
		else
		{
			$this->nowIndex = intval($iNowIndex);
		}
	}
	/**
	*
	*为指定的页面返回地址值
	*@param int iPageNo
	*@return string Url
	*/
	function get_Url($iPageNo = 1)
	{
		return $this->page_Url.$iPageNo;
	}
	/**
	*
	*获取分页显示文字
	*@param string $sStr
	*@return string sUrl
	*/
	function get_Text($sStr)
	{
		return $this->format_Left.$sStr.$this->format_Right;
	}
	/**
	*
	*获取链接地址
	*@param string sUrl
	*@param string sText
	*@param string sStyle
	*@return string
	*/
	function get_Link($sUrl, $sText, $sStyle='')
	{
		$style = empty($sStyle) ? '' : ' class="'.$sStyle.'"';
		if($this->is_Ajax)
		{
			return '<a href="javascript:'.$this->ajax_action_Name.'(\''.$sUrl.'\')" '.$style.'>'.$sText.'</a>';
		}
		else
		{
			return '<a href="'.$sUrl.'" '.$style.'>'.$sText.'</a>';
		}
	}
	 /**
	   * 出错处理方式
	 */
	 function error($function,$errormsg)
	 {
		// die('Error in file <b>'.__FILE__.'</b> ,Function <b>'.$function.'()</b> :'.$errormsg);
	 }
}
class MyPage extends Page
{
    function mypage($array)
 {
      parent::__construct($array);
	  $this->first_page=1;
	  $this->last_page=$this->totalPage;
	  $this->set('format_left','');
	  $this->set('format_right','');
	  $this->format_Left="";
	  $this->format_Right="";
 }

 function show()
 {
	$this->next_Page = '下一页';
	$this->first_Page= '首页';
	$this->last_Page ='尾页';  
	$this->pre_Page  = '上一页'; 	
	$pagestr.=$this->first_page('curr');
	$pagestr.=$this->pre_Page('','curr');
  	$pagestr.=$this->nowbar('','curr');
	$pagestr.=$this->next_Page('','curr');
    $pagestr.=$this->last_page('curr');
  	return $pagestr;
 }
 function simpleShow()
 {
	$this->next_Page = '下一页'; 
	$this->pre_Page  = '上一页'; 	 
	$pagestr.=$this->pre_Page('','curr');
  	$pagestr.=$this->nowbar('','curr');
	$pagestr.=$this->next_Page('','curr');
	return $pagestr;
 }
  function simpleShow2()
 {
	$this->next_Page = '下一页'; 
	$this->pre_Page  = '上一页'; 	 
	$pagestr.=$this->pre_Page('','curr');

	$pagestr.=$this->next_Page('','curr');
	return $pagestr;
 }
}
?>